package com.example.service;

import com.example.entity.Room;
import com.example.mapper.RoomMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoomService {
    private final RoomMapper roomMapper;

    public RoomService(RoomMapper roomMapper) {
        this.roomMapper = roomMapper;
    }

    public List<Room> getAllRooms() {
        return roomMapper.selectAll();
    }

    @Transactional
    public boolean deleteRoom(Integer id) {
        return roomMapper.deleteById(id) > 0;
    }

    @Transactional
    public boolean addRoom(Room room) {
        return roomMapper.insert(room) > 0;
    }

    public Room getRoomById(Integer id) {
        return roomMapper.selectById(id);
    }

    @Transactional
    public boolean updateRoom(Room room) {
        // 确保只更新允许的字段
        Room updateData = new Room();
        updateData.setRoomId(room.getRoomId());
        updateData.setRoomName(room.getRoomName());
        updateData.setRoomType(room.getRoomType());
        
        return roomMapper.update(updateData) > 0;
    }

    public Room getRoomByFamilyIdAndName(Integer familyId, String roomName) {
        return roomMapper.selectByFamilyIdAndName(familyId, roomName);
    }

    public List<Room> getRoomsByFamilyId(Integer familyId) {
        return roomMapper.selectByFamilyId(familyId);
    }
} 